package cn.appsys.controller.back;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.istack.internal.logging.Logger;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.pojo.Page;
import cn.appsys.service.back.appinfo.BackAppInfoService;

@Controller
@RequestMapping("/backend/app")
public class BackAppInfoController {
	
	private Logger logger = Logger.getLogger(BackAppInfoController.class);
	
	@Resource
	private BackAppInfoService backAppInfoService;		//BackAppInfoService对象
	
	@RequestMapping("/queryCategoryByParentId/{parentId}")
	@ResponseBody
	public Object queryCategoryByParentId(@PathVariable("parentId") Integer parentId){
		//调用service层根据当前类别Id查询当前类别下的子级列表信息
		List<AppCategory> categoryList = backAppInfoService.queryappCategoryList(parentId);
		return categoryList;
	}
	
	//显示信息列表
	@RequestMapping("/list")
	public String list(
			@RequestParam(value="querySoftwareName",required=false)String querySoftwareName,
			@RequestParam(value="queryFlatformId",required=false)Integer queryFlatformId,
			@RequestParam(value="queryCategoryLevel1",required=false,defaultValue="0")Integer queryCategoryLevel1,
			@RequestParam(value="queryCategoryLevel2",required=false,defaultValue="0")Integer queryCategoryLevel2,
			@RequestParam(value="queryCategoryLevel3",required=false,defaultValue="0")Integer queryCategoryLevel3,
			@RequestParam(value="currPageNo",required=false,defaultValue = "1")Integer currPageNo,
			Model model){
		
		//调用service层获取总的记录数
		Integer totalCount = backAppInfoService.queryTotalCount(querySoftwareName,
				queryFlatformId,queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3);
		
		//封装Page实体类
		Page page = new Page();
		page.setCurrPageNo(currPageNo);  //当前页面
		page.setPagesize(5);  //每一页显示的数据行数
		page.setTotalCount(totalCount); //总的记录数
		Integer from = (page.getCurrPageNo() - 1) * page.getPagesize();
		//调用service的按条件查询待审核状态的App信息列表信息并分页显示的方法
		List<AppInfo> appInfoList = backAppInfoService.queryAppInfo(querySoftwareName,
				queryFlatformId,queryCategoryLevel1,queryCategoryLevel2,queryCategoryLevel3, 
				from, page.getPagesize());
		
		//调用service层的查询所属平台列表信息
		List<DataDictionary> flatFormList = backAppInfoService.queryAllFlatFrom("APP_FLATFORM");
		
		//一级分类
		List<AppCategory> categoryLevel1List = backAppInfoService.queryappCategoryList(null);
		//二级分类
		List<AppCategory> categoryLevel2List = null;
		//三级分裂
		List<AppCategory> categoryLevel3List = null;
		
		//在一级分类不为空的情况下查询二级分类
		if(queryCategoryLevel1 != null && queryCategoryLevel1 != 0){
			categoryLevel2List = backAppInfoService.queryappCategoryList(queryCategoryLevel1);
		}
		if(queryCategoryLevel2 != null && queryCategoryLevel2 != 0){
			categoryLevel3List = backAppInfoService.queryappCategoryList(queryCategoryLevel2);
		}
		
		//在二级分页不为空的情况CIA查询三级分类
		
		//将查询信息保存到model对象里面(回显)
		model.addAttribute("pages", page);
		model.addAttribute("appInfoList", appInfoList);
		model.addAttribute("flatFormList", flatFormList);
		model.addAttribute("categoryLevel1List", categoryLevel1List);
		model.addAttribute("categoryLevel2List", categoryLevel2List);
		model.addAttribute("categoryLevel3List", categoryLevel3List);
		model.addAttribute("querySoftwareName", querySoftwareName);
		model.addAttribute("queryFlatformId", queryFlatformId);
		model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
		model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
		model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);
		
		return "backend/applist";
		
	}
	
	/*//跳转到backend文件夹的main页面
	@RequestMapping("/main")
	public String main(){
		return "backend/main";
	}*/
	
	//跳转appcheck页面的方法
	@RequestMapping(value="/check",method=RequestMethod.GET)
	public String check(@RequestParam(value="aid",required = false)String appinfoid,
			@RequestParam(value="vid",required=false)String versionId,
			Model model){
		AppInfo appInfo = null;
		AppVersion appVersion = null;
		
		//logger.info("哈哈哈哈==="+appId);
		//logger.info("哈哈哈哈==="+versionId);
		//调用service层的根据ID查询APP信息列表的方法
		appInfo = backAppInfoService.getAppInfo(Integer.parseInt(appinfoid));
		//调用service层根据ID查询版本信息的方法
		appVersion = backAppInfoService.getAppVersionById(Integer.parseInt(versionId));
		
		//保存数据
		model.addAttribute("appInfo",appInfo);
		model.addAttribute("appVersion",appVersion);
		return "backend/appcheck";
	}
	
	//保存修改的方法
	@RequestMapping(value="/checksave",method=RequestMethod.POST)
	public String checkSave(AppInfo appInfo){
		
		logger.info("appInfo=========>"+appInfo.getId());
		logger.info("appInfoId=========>"+appInfo.getStatus());
		if(backAppInfoService.upStatus(appInfo.getStatus(), appInfo.getId())){
			return "redirect:/backend/app/list";
		}
		return "backend/appcheck";
	}
		
}